package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private DBAdapter_zqs dbAdepter_zqs;
    private EditText nameText_zqs;
    private EditText textText_zqs;
    private EditText tableText_zqs;
    private EditText idEntry_zqs;
    private TextView labelView_zqs;
    private TextView displayView_zqs;
    String fileName="test.txt";//文件名称
    String str;//要存取的字符串

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameText_zqs= (EditText)findViewById(R.id.name);
        tableText_zqs = (EditText)findViewById(R.id.table);
        textText_zqs = (EditText)findViewById(R.id.text);
        idEntry_zqs = (EditText)findViewById(R.id.id_entry);
        labelView_zqs = (TextView)findViewById(R.id.label);
        displayView_zqs = (TextView)findViewById(R.id.display);

        Button addButton_zqs = (Button)findViewById(R.id.add);
        Button queryAllButton_zqs = (Button)findViewById(R.id.query_all);
        Button clearButton_zqs = (Button)findViewById(R.id.clear);
        Button deleteAllButton_zqs = (Button)findViewById(R.id.delete_all);
        Button queryButton_zqs = (Button)findViewById(R.id.query_id);
        Button deleteButton_zqs = (Button)findViewById(R.id.delete);
        Button updateButton_zqs = (Button)findViewById(R.id.update);

        addButton_zqs.setOnClickListener(addButtonListener);
        queryAllButton_zqs.setOnClickListener(queryAllButtonListener);
        clearButton_zqs.setOnClickListener(clearButtonListener);
        deleteAllButton_zqs.setOnClickListener(deleteAllButtonListener);
        queryButton_zqs.setOnClickListener(queryButtonListener);
        deleteButton_zqs.setOnClickListener(deleteButtonListener);
        updateButton_zqs.setOnClickListener(updateButtonListener);

        dbAdepter_zqs = new DBAdapter_zqs(this);
        dbAdepter_zqs.open();
        readfile(fileName);
    }

    OnClickListener addButtonListener = new OnClickListener() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onClick(View v) {
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String hehe = dateFormat.format( now );
            People_zqs people_zqs = new People_zqs();
            people_zqs.Name_zqs = nameText_zqs.getText().toString();
            people_zqs.Table_zqs = tableText_zqs.getText().toString();
            people_zqs.Text_zqs = textText_zqs.getText().toString();
            people_zqs.Firsttime_zqs = hehe;
            people_zqs.Finallytime_zqs = hehe;
            long colunm = dbAdepter_zqs.insert(people_zqs);
            if (colunm == -1 ){
                labelView_zqs.setText("添加过程错误！");
            } else {
                labelView_zqs.setText("成功添加数据，ID："+String.valueOf(colunm));
                nameText_zqs.setText("");
                tableText_zqs.setText("");
                textText_zqs.setText("");

            }
        }
    };
    void readfile(String fileName)
    {
        byte[] buffer = new byte[1024];
        FileInputStream in_file=null;
        try{
            in_file = openFileInput(fileName);
            int bytes = in_file.read(buffer);
            str = new String (buffer,0,bytes);
            nameText_zqs.setText(str);
        }
        catch (FileNotFoundException e) {
            java.lang.System.out.print("文件不存在！");
        }
        catch (IOException e) {
            java.lang.System.out.print("IO流错误");
        }
    }

    OnClickListener queryAllButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            People_zqs[] peoples = dbAdepter_zqs.queryAllData();
            if (peoples == null){
                labelView_zqs.setText("数据库中没有数据");
                return;
            }
            labelView_zqs.setText("数据库：");
            String msg = "";
            for (int i = 0 ; i<peoples.length; i++){
                msg += peoples[i].toString()+"\n";
            }
            displayView_zqs.setText(msg);
        }
    };

    OnClickListener clearButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            displayView_zqs.setText("");
        }
    };

    OnClickListener deleteAllButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            dbAdepter_zqs.deleteAllData ();
            String msg = "数据全部删除";
            labelView_zqs.setText(msg);
        }
    };

    OnClickListener queryButtonListener = new OnClickListener() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onClick(View v) {
            int id = Integer.parseInt(idEntry_zqs.getText().toString());
            People_zqs[] peoples = dbAdepter_zqs.queryOneData(id);

            if (peoples == null){
                labelView_zqs.setText("数据库中没有ID为"+String.valueOf(id)+"的数据");
                return;
            }
            labelView_zqs.setText("数据库：");
            displayView_zqs.setText(peoples[0].toString());
        }
    };

    OnClickListener deleteButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            long id = Integer.parseInt(idEntry_zqs.getText().toString());
            long result = dbAdepter_zqs.deleteOneData (id);
            String msg = "删除ID为"+idEntry_zqs.getText().toString()+"的数据" + (result>0?"成功":"失败");
            labelView_zqs.setText(msg);
        }
    };

    OnClickListener updateButtonListener = new OnClickListener() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onClick(View v) {
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
            String hehe = dateFormat.format( now );
            People_zqs people = new People_zqs();
            people.Name_zqs = nameText_zqs.getText().toString();
            people.Table_zqs = tableText_zqs.getText().toString();
            people.Text_zqs = textText_zqs.getText().toString();
            people.Finallytime_zqs = hehe;
            long id = Integer.parseInt(idEntry_zqs.getText().toString());
            long count = dbAdepter_zqs.updateOneData (id, people);
            if (count == -1 ){
                labelView_zqs.setText("更新错误！");
            } else {
                labelView_zqs.setText("更新成功，更新数据"+String.valueOf(count)+"条");

            }
        }
    };
}
